import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 任务管理系统 - 分支功能：查询任务
 * 功能：
 * 1. 按ID查询任务
 * 2. 按描述查询任务
 * 3. 按状态查询任务
 * 4. 按优先级查询任务
 * 5. 按分类查询任务
 * 6. 按标签查询任务
 */
public class TaskQueryManager {

    // 任务类
    private static class Task {
        private int id;
        private String description;
        private String status;
        private String priority;
        private String category;
        private List<String> tags;

        public Task(int id, String description, String status, String priority, String category) {
            this.id = id;
            this.description = description;
            this.status = status;
            this.priority = priority;
            this.category = category;
            this.tags = new ArrayList<>();
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        public String getStatus() {
            return status;
        }

        public String getPriority() {
            return priority;
        }

        public String getCategory() {
            return category;
        }

        public List<String> getTags() {
            return tags;
        }

        public void addTag(String tag) {
            this.tags.add(tag);
        }

        @Override
        public String toString() {
            return "Task{" +
                    "id=" + id +
                    ", description='" + description + '\'' +
                    ", status='" + status + '\'' +
                    ", priority='" + priority + '\'' +
                    ", category='" + category + '\'' +
                    ", tags=" + tags +
                    '}';
        }
    }

    // 查询管理器类
    private static class QueryManager {
        private List<Task> tasks;
        private Map<String, List<Task>> categoryMap;
        private Map<String, List<Task>> tagMap;

        public QueryManager(List<Task> tasks) {
            this.tasks = tasks;
            this.categoryMap = new HashMap<>();
            this.tagMap = new HashMap<>();
            initializeMaps();
        }

        private void initializeMaps() {
            for (Task task : tasks) {
                // 初始化分类映射
                categoryMap.computeIfAbsent(task.getCategory(), k -> new ArrayList<>()).add(task);

                // 初始化标签映射
                for (String tag : task.getTags()) {
                    tagMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(task);
                }
            }
        }

        public List<Task> queryById(int id) {
            List<Task> result = new ArrayList<>();
            for (Task task : tasks) {
                if (task.getId() == id) {
                    result.add(task);
                    break;
                }
            }
            return result;
        }

        public List<Task> queryByDescription(String keyword) {
            List<Task> result = new ArrayList<>();
            for (Task task : tasks) {
                if (task.getDescription().contains(keyword)) {
                    result.add(task);
                }
            }
            return result;
        }

        public List<Task> queryByStatus(String status) {
            List<Task> result = new ArrayList<>();
            for (Task task : tasks) {
                if (task.getStatus().equals(status)) {
                    result.add(task);
                }
            }
            return result;
        }

        public List<Task> queryByPriority(String priority) {
            List<Task> result = new ArrayList<>();
            for (Task task : tasks) {
                if (task.getPriority().equals(priority)) {
                    result.add(task);
                }
            }
            return result;
        }

        public List<Task> queryByCategory(String category) {
            return categoryMap.getOrDefault(category, new ArrayList<>());
        }

        public List<Task> queryByTag(String tag) {
            return tagMap.getOrDefault(tag, new ArrayList<>());
        }
    }

    // 主类
    public static void main(String[] args) {
        List<Task> tasks = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int nextId = 1;

        // 示例任务数据
        Task task1 = new Task(nextId++, "完成项目文档", "进行中", "高", "文档");
        task1.addTag("紧急");
        task1.addTag("文档");

        Task task2 = new Task(nextId++, "修复登录模块的Bug", "未完成", "中", "Bug修复");
        task2.addTag("Bug");
        task2.addTag("登录");

        Task task3 = new Task(nextId++, "设计用户界面", "完成", "高", "设计");
        task3.addTag("UI");
        task3.addTag("设计");

        tasks.add(task1);
        tasks.add(task2);
        tasks.add(task3);

        QueryManager queryManager = new QueryManager(tasks);

        while (true) {
            System.out.println("\n===== 任务查询系统 =====");
            System.out.println("1. 按ID查询任务");
            System.out.println("2. 按描述查询任务");
            System.out.println("3. 按状态查询任务");
            System.out.println("4. 按优先级查询任务");
            System.out.println("5. 按分类查询任务");
            System.out.println("6. 按标签查询任务");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入任务ID: ");
                    int taskId = scanner.nextInt();
                    scanner.nextLine(); // 消耗换行符
                    List<Task> resultById = queryManager.queryById(taskId);
                    displayResults(resultById);
                    break;
                case 2:
                    System.out.print("请输入描述关键词: ");
                    String keyword = scanner.nextLine();
                    List<Task> resultByDescription = queryManager.queryByDescription(keyword);
                    displayResults(resultByDescription);
                    break;
                case 3:
                    System.out.print("请输入任务状态 (完成/进行中/未完成): ");
                    String status = scanner.nextLine();
                    List<Task> resultByStatus = queryManager.queryByStatus(status);
                    displayResults(resultByStatus);
                    break;
                case 4:
                    System.out.print("请输入任务优先级 (高/中/低): ");
                    String priority = scanner.nextLine();
                    List<Task> resultByPriority = queryManager.queryByPriority(priority);
                    displayResults(resultByPriority);
                    break;
                case 5:
                    System.out.print("请输入分类名称: ");
                    String category = scanner.nextLine();
                    List<Task> resultByCategory = queryManager.queryByCategory(category);
                    displayResults(resultByCategory);
                    break;
                case 6:
                    System.out.print("请输入标签名称: ");
                    String tag = scanner.nextLine();
                    List<Task> resultByTag = queryManager.queryByTag(tag);
                    displayResults(resultByTag);
                    break;
                case 0:
                    System.out.println("退出系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    // 显示查询结果
    private static void displayResults(List<Task> results) {
        if (results.isEmpty()) {
            System.out.println("没有找到符合条件的任务");
        } else {
            System.out.println("\n===== 查询结果 =====");
            for (Task task : results) {
                System.out.println(task);
            }
        }
    }
}